import torch
import torch.nn as nn
import torch.nn.functional as F
torch.backends.cudnn.benchmark = True
print(torch.cuda.is_available())

class NetMNIST(nn.Module):
    def __init__(self):
        super(NetMNIST, self).__init__()
        # 卷积层
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        # 全连接层
        self.fc1 = nn.Linear(20 * 4 * 4, 50)  # 修改为 4*4，因为池化后尺寸变小
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        # 卷积层 + 激活 + 池化
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2(x), 2))
        # x.size() 应该是 [batch_size, 20, 4, 4]，根据这个计算全连接层的输入尺寸
        x = x.view(-1, 20 * 4 * 4)  # 修正尺寸
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

class NetCIFAR(nn.Module):
    def __init__(self):
        super(NetCIFAR, self).__init__()
        # 卷积层
        self.conv1 = nn.Conv2d(3, 6, kernel_size=5)
        self.conv2 = nn.Conv2d(6, 16, kernel_size=5)
        # 全连接层
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # 卷积层 + 激活 + 池化
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2(x), 2))
        # x.size() 应该是 [batch_size, 16, 5, 5]，计算全连接层的输入尺寸
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
